package efr.dao.impl;

import java.util.List;

import org.hibernate.Session;

import efr.dao.RimaDao;
import efr.entity.Rima;
import efr.util.HibernateUtil;

public class RimaDaoImpl implements RimaDao {

	@Override
	public List<Rima> getAllRime() {
		List<Rima> rimas = null;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		rimas = session.createQuery("FROM Rima").list();
		session.getTransaction().commit();
		return rimas;
	}

	@Override
	public Rima getRima(int id) {
		Rima rima = null;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		rima = (Rima) session.get(Rima.class, id);
		session.getTransaction().commit();
		return rima;
	}

	@Override
	public void updateRima(Rima rima) throws Exception{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.update(rima);
		session.getTransaction().commit();
	}

	@Override
	public void deleteRima(Rima rima) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.delete(rima);
		session.getTransaction().commit();
	}

	@Override
	public Integer addRima(Rima rima) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Integer id = (Integer)session.save(rima);
		session.getTransaction().commit();
		return id;
	}

}
